/*
 * @Author: Kiro AI Assistant
 * @Date: 2025-01-10
 * @Description: 播放列表页面使用示例
 */

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../viewmodels/music_player_view_model.dart';
import '../models/music_item_model.dart';
import '../pages/playlist_page_view.dart';

/// 播放列表页面使用示例
class PlaylistPageExample extends StatelessWidget {
  const PlaylistPageExample({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('播放列表页面示例'),
        backgroundColor: Colors.black,
        foregroundColor: Colors.white,
      ),
      body: Container(
        padding: const EdgeInsets.all(20),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const Text(
              '播放列表页面功能演示',
              style: TextStyle(
                fontSize: 24,
                fontWeight: FontWeight.bold,
              ),
            ),
            const SizedBox(height: 20),

            const Text(
              '功能特性：',
              style: TextStyle(
                fontSize: 18,
                fontWeight: FontWeight.w600,
              ),
            ),
            const SizedBox(height: 10),

            const Text('• 垂直滑动切歌功能'),
            const Text('• 当前播放页面实时进度显示'),
            const Text('• 非当前播放页面静态信息显示'),
            const Text('• 70% 阈值预加载机制'),
            const Text('• 播放列表管理（添加、删除、清空）'),

            const SizedBox(height: 30),

            // 操作按钮
            Wrap(
              spacing: 10,
              runSpacing: 10,
              children: [
                ElevatedButton(
                  onPressed: () => _loadSamplePlaylist(context),
                  child: const Text('加载示例播放列表'),
                ),
                ElevatedButton(
                  onPressed: () => _openPlaylistPage(context),
                  child: const Text('打开播放列表页面'),
                ),
                ElevatedButton(
                  onPressed: () => _clearPlaylist(context),
                  style: ElevatedButton.styleFrom(
                    backgroundColor: Colors.red,
                    foregroundColor: Colors.white,
                  ),
                  child: const Text('清空播放列表'),
                ),
              ],
            ),

            const SizedBox(height: 30),

            // 当前播放列表状态
            Consumer<MusicPlayerViewModel>(
              builder: (context, viewModel, child) {
                return Card(
                  child: Padding(
                    padding: const EdgeInsets.all(16),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          '当前播放列表状态',
                          style: Theme.of(context).textTheme.titleMedium,
                        ),
                        const SizedBox(height: 10),
                        Text('歌曲数量: ${viewModel.playlist.length}'),
                        Text('当前播放索引: ${viewModel.currentIndex}'),
                        if (viewModel.hasCurrentTrack)
                          Text(
                              '当前播放: ${viewModel.currentTrackTitle} - ${viewModel.currentTrackArtist}'),
                      ],
                    ),
                  ),
                );
              },
            ),
          ],
        ),
      ),
    );
  }

  /// 加载示例播放列表
  void _loadSamplePlaylist(BuildContext context) {
    final viewModel = context.read<MusicPlayerViewModel>();

    final samplePlaylist = [
      const MusicItemModel(
        id: 'sample_1',
        title: '示例歌曲 1',
        artist: '示例艺术家 1',
        album: '示例专辑 1',
        audioUrl: 'https://example.com/sample1.mp3',
        duration: Duration(minutes: 3, seconds: 30),
        sourceType: 'sample',
      ),
      const MusicItemModel(
        id: 'sample_2',
        title: '示例歌曲 2',
        artist: '示例艺术家 2',
        album: '示例专辑 2',
        audioUrl: 'https://example.com/sample2.mp3',
        duration: Duration(minutes: 4, seconds: 15),
        sourceType: 'sample',
      ),
      const MusicItemModel(
        id: 'sample_3',
        title: '示例歌曲 3',
        artist: '示例艺术家 3',
        album: '示例专辑 3',
        audioUrl: 'https://example.com/sample3.mp3',
        duration: Duration(minutes: 2, seconds: 45),
        sourceType: 'sample',
      ),
      const MusicItemModel(
        id: 'sample_4',
        title: '示例歌曲 4',
        artist: '示例艺术家 4',
        album: '示例专辑 4',
        audioUrl: 'https://example.com/sample4.mp3',
        duration: Duration(minutes: 5, seconds: 20),
        sourceType: 'sample',
      ),
      const MusicItemModel(
        id: 'sample_5',
        title: '示例歌曲 5',
        artist: '示例艺术家 5',
        album: '示例专辑 5',
        audioUrl: 'https://example.com/sample5.mp3',
        duration: Duration(minutes: 3, seconds: 55),
        sourceType: 'sample',
      ),
    ];

    viewModel.setPlaylist(samplePlaylist);

    ScaffoldMessenger.of(context).showSnackBar(
      const SnackBar(
        content: Text('已加载示例播放列表'),
        duration: Duration(seconds: 2),
      ),
    );
  }

  /// 打开播放列表页面
  void _openPlaylistPage(BuildContext context) {
    final viewModel = context.read<MusicPlayerViewModel>();

    if (viewModel.playlist.isEmpty) {
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(
          content: Text('播放列表为空，请先加载示例播放列表'),
          duration: Duration(seconds: 2),
        ),
      );
      return;
    }

    Navigator.of(context).push(
      MaterialPageRoute(
        builder: (context) => const PlaylistPageView(),
      ),
    );
  }

  /// 清空播放列表
  void _clearPlaylist(BuildContext context) {
    final viewModel = context.read<MusicPlayerViewModel>();
    viewModel.clearPlaylist();

    ScaffoldMessenger.of(context).showSnackBar(
      const SnackBar(
        content: Text('播放列表已清空'),
        duration: Duration(seconds: 2),
      ),
    );
  }
}
